package org.i1510.common.utils;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

public class PrintWriterUtils {
	public   int _0 = 0;

	public  String EXPIRES = "Expires";

	public   String PRAGMA = "Pragma";

	public   String NO_CACHE = "no-cache";

	public   String CACHE_CONTROL = "Cache-Control";

	public   String JSON_TYPE = "application/json; charset=utf-8";

	public   String HTML_TYPE = "text/html; charset=utf-8";

	public   String XML_TYPE = "text/xml; charset=utf-8";

	public   String PLAIN_TYPE = "text/plain; charset=utf-8";
	public void sendPrintWriterUtils(HttpServletResponse response,
			String message,String mode) {
		
		try {
			response.setHeader(CACHE_CONTROL, NO_CACHE);
			response.setHeader(PRAGMA, NO_CACHE);
			response.setDateHeader(EXPIRES, _0);
			response.setCharacterEncoding("UTF-8");
			if(null != mode && mode.toLowerCase().trim().equals("json")){
				response.setContentType(JSON_TYPE);
			}else if(null != mode && mode.toLowerCase().trim().equals("xml")){
				response.setContentType(XML_TYPE);
			}else if(null != mode && mode.toLowerCase().trim().equals("plain")){
				response.setContentType(PLAIN_TYPE);
			}else if(null != mode && mode.toLowerCase().trim().equals("alert")){
				message ="<meta http-equiv='content-type' content='text/html; charset=UTF-8'><script>alert('"+message+"')</script>";
			}else{
				response.setContentType(HTML_TYPE);
			}
			
			PrintWriter out = response.getWriter();
			out.write(message);
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
	}
	
}
